<?php

	class product_code_DAO extends CI_Model{

		public function __construct(){
			parent::__construct();
			$this->load->database();
		}
		
		public function search($keyword){
			$where = "code like '%{$keyword}%'";
			$this->db->where($where);
			$query = $this->db->get("product_code");
			return $query->result_array();
		}
		
		public function getCode($category_level_2_id, $category_level_3_id, $product_fit_id, $product_crotch_id){
			$this->db->where('category_level_2_id', $category_level_2_id);
			$this->db->where('category_level_3_id', $category_level_3_id);
			$this->db->where('product_fit_id', $product_fit_id);
			$this->db->where('product_crotch_id', $product_crotch_id);
			$query = $this->db->get("product_code");
			$product_code_list = $query->result_array();
			if(sizeof($product_code_list) == 1){
				$product_code = $product_code_list[0];
				return $product_code;
			} else {
				return null;
			}
		}
		
		public function get($id){
			$this->db->where('id', $id);
			$query = $this->db->get("product_code");
			if($query->num_rows() == 1){
				$tempQuery = $query->result();
				return $tempQuery[0];
			} else {
				return null;
			}
		}
		
		public function getByCode($code){
			$this->db->where('code', $code);
			$query = $this->db->get("product_code");
			$product_code_list = $query->result_array();
			if(sizeof($product_code_list) == 1){
				$product_code = $product_code_list[0];
				return $product_code;
			} else {
				return null;
			}
		}
		
		public function add($category){
			$this->db->insert("product_code", $category);
			return ($this->db->affected_rows() != 1) ? false : true;
		}
		
		public function update($id, $category_level_2_id, $category_level_3_id, $product_fit_id, $product_crotch_id, $code, $description){
			$data = array(
				'category_level_2_id' => $category_level_2_id,
				'category_level_3_id' => $category_level_3_id,
				'product_fit_id' => $product_fit_id,
				'product_crotch_id' => $product_crotch_id,
				'code' => $code,
				'description' => $description
            );
			$this->db->where('id', $id);
			$this->db->update('product_code', $data);
			return ($this->db->affected_rows() != 1) ? false : true;
		}
		
		public function delete($id){
			$this->db->where('id', $id);
			$this->db->delete('product_code');
			return ($this->db->affected_rows() != 1) ? false : true;
		}
	}

?>